{{define "content"}}
<div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
    <div class="px-4 py-6 sm:px-0">
        <div class="bg-white shadow rounded-lg p-6">
            <h2 class="text-2xl font-bold text-gray-800 mb-6">
                <i class="fas fa-cog mr-2 text-blue-600"></i>系统设置
            </h2>

            {{if .Success}}
            <div class="mb-6 bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative" role="alert">
                <span class="block sm:inline">设置已成功保存！</span>
            </div>
            {{end}}

            <form action="/settings" method="POST" class="space-y-6">
                <div>
                    <label for="prefix" class="block text-sm font-medium text-gray-700">文章生成前缀</label>
                    <div class="mt-1">
                        <textarea id="prefix" name="prefix" rows="3" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md">{{.prefix}}</textarea>
                    </div>
                    <p class="mt-2 text-sm text-gray-500">设置生成文章时使用的提示词前缀，这将影响生成文章的风格和内容。</p>
                </div>

                <div>
                    <label for="article_length" class="block text-sm font-medium text-gray-700">文章长度</label>
                    <div class="mt-1">
                        <select id="article_length" name="article_length" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md">
                            <option value="short" {{if eq .articleLength "short"}}selected{{end}}>短篇 (500-1000字)</option>
                            <option value="medium" {{if eq .articleLength "medium"}}selected{{end}}>中篇 (1000-2000字)</option>
                            <option value="long" {{if eq .articleLength "long"}}selected{{end}}>长篇 (2000-3000字)</option>
                        </select>
                    </div>
                    <p class="mt-2 text-sm text-gray-500">设置生成文章的长度范围。</p>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700">栏目设置</label>
                    <div class="mt-1" id="categories-container">
                        {{range .categories}}
                        <div class="flex items-center space-x-2 mb-2 category-item">
                            <input type="text" name="category_ids[]" value="{{.ID}}" placeholder="栏目ID" 
                                class="shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm border-gray-300 rounded-md">
                            <input type="text" name="category_names[]" value="{{.Name}}" placeholder="栏目名称" 
                                class="shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm border-gray-300 rounded-md">
                            <button type="button" onclick="removeCategory(this)" class="text-red-600 hover:text-red-800">
                                <i class="fas fa-times"></i>
                            </button>
                        </div>
                        {{end}}
                    </div>
                    <button type="button" onclick="addCategory()" class="mt-2 inline-flex items-center px-3 py-1 border border-transparent text-sm font-medium rounded-md text-blue-600 bg-blue-100 hover:bg-blue-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                        <i class="fas fa-plus mr-1"></i>添加栏目
                    </button>
                    <p class="mt-2 text-sm text-gray-500">设置文章栏目，可以添加多个栏目。</p>
                </div>

                <div>
                    <label for="default_category" class="block text-sm font-medium text-gray-700">默认栏目</label>
                    <div class="mt-1">
                        <select id="default_category" name="default_category" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md">
                            <option value="">请选择默认栏目</option>
                            {{range .categories}}
                            <option value="{{.ID}}" {{if eq $.defaultCategory .ID}}selected{{end}}>{{.Name}}</option>
                            {{end}}
                        </select>
                    </div>
                    <p class="mt-2 text-sm text-gray-500">设置默认的文章栏目，当生成文章时未选择栏目时使用此栏目。</p>
                </div>

                <div class="flex justify-end">
                    <button type="submit" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                        <i class="fas fa-save mr-2"></i>保存设置
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{{end}}

{{define "scripts"}}
<script>
function addCategory() {
    const container = document.getElementById('categories-container');
    const div = document.createElement('div');
    div.className = 'flex items-center space-x-2 mb-2 category-item';
    div.innerHTML = `
        <input type="text" name="category_ids[]" placeholder="栏目ID" 
            class="shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm border-gray-300 rounded-md">
        <input type="text" name="category_names[]" placeholder="栏目名称" 
            class="shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm border-gray-300 rounded-md">
        <button type="button" onclick="removeCategory(this)" class="text-red-600 hover:text-red-800">
            <i class="fas fa-times"></i>
        </button>
    `;
    container.appendChild(div);
    updateDefaultCategoryOptions();
}

function removeCategory(button) {
    button.closest('.category-item').remove();
    updateDefaultCategoryOptions();
}

function updateDefaultCategoryOptions() {
    const defaultSelect = document.getElementById('default_category');
    const currentValue = defaultSelect.value;
    const categories = [];
    
    // 收集所有栏目
    document.querySelectorAll('.category-item').forEach(item => {
        const id = item.querySelector('input[name="category_ids[]"]').value;
        const name = item.querySelector('input[name="category_names[]"]').value;
        if (id && name) {
            categories.push({id, name});
        }
    });

    // 更新默认栏目下拉框
    defaultSelect.innerHTML = '<option value="">请选择默认栏目</option>';
    categories.forEach(cat => {
        const option = document.createElement('option');
        option.value = cat.id;
        option.textContent = cat.name;
        if (cat.id === currentValue) {
            option.selected = true;
        }
        defaultSelect.appendChild(option);
    });
}
</script>
{{end}}